/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.adv.web.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.LogFactory;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkertUtil {

    private static final org.apache.commons.logging.Log logger = LogFactory.getLog(FreeMarkertUtil.class);

    /**
     * templatePath 模板文件存放路径,templateName 模板文件名称,filename 生成的文件名称
     * 
     * @param templatePath
     * @param templateName
     * @param fileName
     * @param root
     */
    public static void analysisTemplate(String templatePath, String templateName, String fileName, Map<?, ?> root)
                                                                                                                  throws TemplateException {
        try {
            File htmlFile = new File(fileName);
            File parentFile = htmlFile.getParentFile();
            if (!parentFile.exists()) {
                if (!parentFile.mkdirs()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("创建文件夹失败:" + parentFile);
                    }
                }
            }

            // 初使化FreeMarker配置
            Configuration config = new Configuration();
            // 设置要解析的模板所在的目录，并加载模板文件
            config.setDirectoryForTemplateLoading(new File(templatePath));
            // 设置包装器，并将对象包装为数据模型
            config.setObjectWrapper(new DefaultObjectWrapper());
            config.setNumberFormat("0"); // 取消数字的自动格式化

            // 获取模板,并设置编码方式，这个编码必须要与页面中的编码格式一致
            // 否则会出现乱码
            Template template = config.getTemplate(templateName, "UTF-8");
            // 合并数据模型与模板
            FileOutputStream fos = new FileOutputStream(fileName);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            template.process(root, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("文件错误: " + e.getMessage());
                logger.debug("模板文件: " + templateName);
                logger.debug("静态文件: " + fileName);
            }
        } catch (TemplateException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("静态化错误: " + e.getMessage());
                logger.debug("模板文件: " + templateName);
                logger.debug("静态文件: " + fileName);
            }
            // throw new ParamException(MessageInfo.INFO_CREATE_HTML_ERROR);
            throw e;
        }
    }

}
